#!/usr/bin/python

e24z = (1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 2.7, 3.0, 3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1)
e24  = []
for v in e24z:
    e24.append(v / 10.0)
for v in e24z:
    e24.append(v)
for v in e24z:
    e24.append(v * 10)
for v in e24z:
    e24.append(v * 100)

def findNearest(val):
    d = 100
    a = 0
    for v in e24:
        if abs(val-v) < d:
            a = v
            d = abs(val-v)
    return a


best = {}
mdu  = 100

for r0 in e24z:
    dsum = 0

    # To find out constants c1, c2, solve algebraic system. In Maxima:
    # algsys([0.875=3*R0*R2 / ((R1+R0)*R2+R0*R1), 0.968=3*(R0*R2+R0*R1)/((R1+R0)*R2+R0*R1)], [R1,R2]);
    c1 = 2032.0 / 875.0
    c2 = 2032.0 / 93.0

    r1 = findNearest(r0*c1)
    r2 = findNearest(r0*c2)

    u1 = 3.0 * r0*r2 / ((r1+r0)*r2 + r0*r1)
    u2 = 3.0 * (r0*r2 + r0*r1) / ((r1+r0)*r2 + r0*r1)
    us = (u1+u2) / 2.0
    du = abs(470.0/510.0 - us)

    print "r0: %5.1f, r1: %5.1f, r2: %5.1f, du:%5.1f mV" % (r0, r1, r2, du*1000)

    if du < mdu:
        mdu  = du
        best['r0'] = r0
        best['r1'] = r1
        best['r2'] = r2
        best['u1'] = u1
        best['u2'] = u2

print best
